package homework;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @classname:demo01
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/9 17:14
 * @version: 0.1
 * @since:1.8
 */
public class TimeUtil {
    private TimeUtil() {
    }

    private static final String PATTERN = "yyyy-MM-dd HH-mm-ss";
    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN);

    public static void main(String[] args) {
//        String date = "2020-12-12 23-12-23";
//        Date date1 = StringToDate(date);
//        System.out.println(date1);
//        LocalDateTime localDateTime = StringToDLocalDateTime(date);
//        System.out.println(localDateTime);
//        Date date = new Date();
//        String string = dateToString(date);
//        System.out.println(string);
//        LocalDateTime localDateTime = LocalDateTime.now();
//        System.out.println(localDateTimeToString(localDateTime));

    }

    private static String localDateTimeToString( LocalDateTime localDateTime ){
        String format = localDateTime.format(dateTimeFormatter);
        return format;
    }

    private static String dateToString(Date date) {
        DateFormat dateFormat = new SimpleDateFormat(PATTERN);
        String format = dateFormat.format(date);
        return format;
    }

    private static LocalDateTime StringToDLocalDateTime(String date) {
        LocalDateTime parse = LocalDateTime.parse(date, dateTimeFormatter);
        return parse;
    }


    private static Date StringToDate(String date) {
        DateFormat dateFormat = new SimpleDateFormat(PATTERN);
        Date parse = null;
        try {
            parse = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
}
